package com.jiezhuo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.JzgNewMenu;
import com.jiezhuo.entity.cmeduSchool.XxdwUserlist;
import com.jiezhuo.handler.RzException;
import com.jiezhuo.service.cmeduSchool.XxdwUserlistService;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务控制器
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-06 08:59:27
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "学校系统管理--用户")
@RequestMapping("/xxdwUserlist")
public class XxdwUserlistController {
    private final XxdwUserlistService xxdwUserlistService;

    @ApiOperation("获取当前用户信息")
    @GetMapping("getCurrentUserInfo")
    public R getCurrentUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId = loginUser.getUser().getUserid();
        QueryWrapper<XxdwUserlist> xxdwUserlistQueryWrapper = new QueryWrapper<>();
        xxdwUserlistQueryWrapper.eq("UserId", userId);
        XxdwUserlist xxdwUserlistServiceOne = xxdwUserlistService.getOne(xxdwUserlistQueryWrapper);
        return R.ok().data("menuTree", xxdwUserlistServiceOne);
    }

    @ApiOperation("获取系统管理员")
    @GetMapping("getSysUserAdmin")
    public R getSysUserAdmin(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> list = xxdwUserlistService.getSysUserAdmin(map);
        return R.ok().data("SysUserAdmin", list);
    }

    @ApiOperation("删除系统用户")
    @PostMapping("deleteSysUserAdmin")
    public R DeleteSysUserAdmin(@RequestBody XxdwUserlist xxdwUserlist) {
        if (xxdwUserlist.getUserid() == null || "".equals(xxdwUserlist.getUserid())) {
            throw new RzException(500, "系统用户id不能为null");
        }
        QueryWrapper<XxdwUserlist> userlistQueryWrapper = new QueryWrapper<>();
        userlistQueryWrapper.eq("UserId", xxdwUserlist.getUserid());

        boolean remove = xxdwUserlistService.remove(userlistQueryWrapper);
        return R.ok().data("start", remove);
    }

    @ApiOperation("修改系统用户")
    @PostMapping("updateSysUserAdmin")
    public R updateSysUserAdmin(@RequestBody XxdwUserlist xxdwUserlist) {
        if (StringUtils.isStringNotExist(xxdwUserlist.getUserid())) {
            throw new RzException(500, "系统用户id不能为null");
        }
        if (StringUtils.isStringNotExist(xxdwUserlist.getOldUserid())) {
            throw new RzException(500, "系统用户id不能为null");
        }
        UpdateWrapper<XxdwUserlist> userlistupdateWrapper = new UpdateWrapper<>();
        userlistupdateWrapper.eq("UserId", xxdwUserlist.getOldUserid());
        boolean update = xxdwUserlistService.update(xxdwUserlist, userlistupdateWrapper);
        return R.ok().data("start", update);
    }


    @ApiOperation("获取老师信息")
    @GetMapping("getTeacherInfo")
    public R getTeacherInfo(@RequestParam String gzXqdm) {
        List<Map<String, Object>> list = xxdwUserlistService.getTeacherInfo(gzXqdm);
        return R.ok().data("teacher", list);
    }

    @ApiOperation("增加系统用户")
    @PostMapping("insertSysUserAdmin")
    public R insertSysUserAdmin(@RequestBody XxdwUserlist xxdwUserlist) {
        boolean update = xxdwUserlistService.save(xxdwUserlist);
        return R.ok().data("start", update);
    }
}